<?php

namespace eu\freeplace\validator;

/**
 * Validation Service for Variables.
 *
 * @author Elmar Dott
 * @version 1.0
 */
class Validator {
    
    const CLASS_NAME = "Validator";

    //Singelton Pattern =============================================
    static private $instance = null;
    static public function getInstance() {
         if (null === self::$instance) {
             self::$instance = new self;
         }
         return self::$instance;
    }
    /** Private Constructor implements a Singelton. */
    private function __construct() {
         global $logger;
         $logger->log("Calling instance of ".self::CLASS_NAME."() [Singelton]", "NOTICE");
    }
    #================================================================

    ### REGEX VALIDATION PATTERN
    ###  * = 0 or more quantifier
    ###  + = 1 or more quantifier
    ###  ? = 0 or 1 quantifier

    // Basic Datatypes - not null
    const BOOLEAN = "/^(true|false)|(TRUE|FALSE)|(0|1)$/";
    const NUMBER  = "/^\-?[0-9]+$/";
    const TEXT    = "/^[a-zA-Z0-9öäüÜÖÄß ?!.;,()&�%=-_:+]+$/";

    const CONTEXT  = "/^[0-9a-zA-Z_-]{1,50}$/";
    const RGB_COLOR = "/^#[0-9a-fA-F]{3,6}$/";
    const EMAIL = "";

    /**
     * Basic Secure Function
     *
     * @param  String inputstream
     * @return String inputstream
     */
    public function makeSecure($string) {
        $string = strip_tags($string);    // remove HTML & PHP Tags
        $string = stripcslashes($string); // remove "/"
        $string = stripslashes($string);  // remove "\"
        return $string;
    }
    
    /**
     * Check if the Stig is not empty.
     * 
     * @param String $string
     * @return $string 
     */
    public function isNotEmpty($string) {
        if(strlen($string) > 0 && sizeof($string) > 0)
            return $string;
        else 
            return null;
    }
    
    /**
     * Validate a inputstring with a regex pattern. The secure Parameter cut all
     * dangerous symbols (e.g. <> ) off.
     *
     * @global Logger $logger
     * @param String  $pattern
     * @param String  $input
     * @param Boolean $secure [optional]
     * @return String $inputstream
     */
    public function validate($pattern, $input, $secure="true") {

        global $logger;
        $input      = trim($input); // delete whitespace
        $validation = null;
        
        try {

            $logger->log(self::CLASS_NAME."().validate(".$pattern.")", "DEBUG");

            // if the regex not empty AND the result of the pattern match
            // false -> then throw an exception.
            if ($pattern != "" && preg_match($pattern, $input) === 0) {
                throw new \Exception("Validation Error: INVALID INPUT");

            } else {

                if ($secure == true) {
                    $validation = $this->makeSecure($input);
                } else {
                    $validation = $input;
                }
                $logger->log(self::CLASS_NAME."(): Validation for [".$input."] sucsessful.", "DEBUG");
            }

        } catch (\Exception $e) {
            
            $logger->log(self::CLASS_NAME."(): ".$e->getMessage(), "ERROR");
            return 0;
        }
        
        return $validation;
    }
    
}//CLASS

?>